package com.hehe.httpurlconnectiontest2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.LinkedList;

/**
 * Created by lenove on 2017/11/11.
 *
 * 显示传感器列表得Adapter
 */

public class SensorBaseAdapter extends BaseAdapter {

    LinkedList<YeelinkSensor> data = null;
    Context mContext;
    int DevicePosition;

    SensorBaseAdapter(LinkedList<YeelinkDecives> decives, Context mContext,int DevicePosition){
        this.data = decives.get(DevicePosition).getSensorLinkedList();
        this.mContext = mContext;
        this.DevicePosition = DevicePosition;
    }

    @Override
    public int getCount() {
        if (data == null){
            return 0;
        }else {
            return data.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sensor,parent,false);
            holder.SensorName = (TextView)convertView.findViewById(R.id.SensorName);
            holder.SensorFlashTime = (TextView)convertView.findViewById(R.id.SensorFlashTime);
            holder.SensorDataValue = (TextView)convertView.findViewById(R.id.SensorDataValue);
            holder.SensorDataUnit = (TextView)convertView.findViewById(R.id.SensorDataUnit);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //TODO 这里的getSensor_title应该改下改为SensorName
        holder.SensorName.setText(data.get(position).getSensor_title());
        holder.SensorFlashTime.setText(data.get(position).getDataPoint().getTime());
        holder.SensorDataValue.setText(data.get(position).getDataPoint().getValue());
        holder.SensorDataUnit.setText(data.get(position).getUnit_symbol());

        return convertView;
    }

    private static class ViewHolder{
        TextView SensorName,SensorFlashTime,SensorDataValue,SensorDataUnit;
    }

    public void flash(){
        notifyDataSetChanged();
    }

//    //设置传感器的值
//    public void setDataValue(int position,String value){
//        if(data != null){
//            data.get(position).getDataPoint().getValue();
//            notifyDataSetChanged();
//        }
//    }

//    //设置单位
//    public void setDataUnit(int position,String unit){
//        if (data != null){
//            data.get(position).setUnit_symbol(unit);
//            notifyDataSetChanged();
//        }
//    }

//    //设置时间
//    public void setTime(int position , String timestamp){
//        if (data!=null){
//            data.get(position).getDataPoint().getTimestamp();
//            notifyDataSetChanged();
//        }
//    }

//    //设置指定的传感器名
//    public void setSensorName(int position ,String name){
//        if (data!=null){
//            data.get(position).setSensor_title(name);
//            notifyDataSetChanged();
//        }
//    }

//    //添加传感器
//    public void addSensor(YeelinkSensor sensors){
//        if (data==null){
//            data = new LinkedList<YeelinkSensor>();
//            data.addLast(sensors);
//            notifyDataSetChanged();
//        }else {
//            data.addLast(sensors);
//            notifyDataSetChanged();
//        }
//    }

//    //直接更新数组
//    public void setDataList(LinkedList<YeelinkSensor> list){
//        if (data==null){
//            data = new LinkedList<YeelinkSensor>();
//            data = list;
//            notifyDataSetChanged();
//        }else {
//            data = list;
//            notifyDataSetChanged();
//        }
//    }
}
